{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# assignment_task_sizes_sat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/sat/assignment_task_sizes_sat.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/ortools/sat/samples/assignment_task_sizes_sat.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "Solves a simple assignment problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.sat.python import cp_model\n",
    "\n",
    "\n",
    "def main() -> None:\n",
    "    # Data\n",
    "    costs = [\n",
    "        [90, 76, 75, 70, 50, 74, 12, 68],\n",
    "        [35, 85, 55, 65, 48, 101, 70, 83],\n",
    "        [125, 95, 90, 105, 59, 120, 36, 73],\n",
    "        [45, 110, 95, 115, 104, 83, 37, 71],\n",
    "        [60, 105, 80, 75, 59, 62, 93, 88],\n",
    "        [45, 65, 110, 95, 47, 31, 81, 34],\n",
    "        [38, 51, 107, 41, 69, 99, 115, 48],\n",
    "        [47, 85, 57, 71, 92, 77, 109, 36],\n",
    "        [39, 63, 97, 49, 118, 56, 92, 61],\n",
    "        [47, 101, 71, 60, 88, 109, 52, 90],\n",
    "    ]\n",
    "    num_workers = len(costs)\n",
    "    num_tasks = len(costs[0])\n",
    "\n",
    "    task_sizes = [10, 7, 3, 12, 15, 4, 11, 5]\n",
    "    # Maximum total of task sizes for any worker\n",
    "    total_size_max = 15\n",
    "\n",
    "    # Model\n",
    "    model = cp_model.CpModel()\n",
    "\n",
    "    # Variables\n",
    "    x = {}\n",
    "    for worker in range(num_workers):\n",
    "        for task in range(num_tasks):\n",
    "            x[worker, task] = model.new_bool_var(f\"x[{worker},{task}]\")\n",
    "\n",
    "    # Constraints\n",
    "    # Each worker is assigned to at most one task.\n",
    "    for worker in range(num_workers):\n",
    "        model.add(\n",
    "            sum(task_sizes[task] * x[worker, task] for task in range(num_tasks))\n",
    "            <= total_size_max\n",
    "        )\n",
    "\n",
    "    # Each task is assigned to exactly one worker.\n",
    "    for task in range(num_tasks):\n",
    "        model.add_exactly_one(x[worker, task] for worker in range(num_workers))\n",
    "\n",
    "    # Objective\n",
    "    objective_terms = []\n",
    "    for worker in range(num_workers):\n",
    "        for task in range(num_tasks):\n",
    "            objective_terms.append(costs[worker][task] * x[worker, task])\n",
    "    model.minimize(sum(objective_terms))\n",
    "\n",
    "    # Solve\n",
    "    solver = cp_model.CpSolver()\n",
    "    status = solver.solve(model)\n",
    "\n",
    "    # Print solution.\n",
    "    if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:\n",
    "        print(f\"Total cost = {solver.objective_value}\\n\")\n",
    "        for worker in range(num_workers):\n",
    "            for task in range(num_tasks):\n",
    "                if solver.boolean_value(x[worker, task]):\n",
    "                    print(\n",
    "                        f\"Worker {worker} assigned to task {task}.\"\n",
    "                        + f\" Cost = {costs[worker][task]}\"\n",
    "                    )\n",
    "    else:\n",
    "        print(\"No solution found.\")\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
